﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinook.Facade;
using Chinook.Core.DaoInterface;
using Chinook.Core.DomainModel;
using Chinook.Facade.ViewModel;
using Chinook.Facade.Messaging;
using Chinook.Facade.Ioc;
using Base.Utils;
using Base.Utils.Logging;
using Base.Utils.Factory;

namespace Chinook.Facade
{
   public class EmployeeAppService : IEmployeeAppService
    {
       private IEmployeeDao _employeeDao;

       public IEmployeeDao EmployeeDao
       {
           get{
               if (_employeeDao != null)
                   return _employeeDao;
               _employeeDao = new FacadeExposer().EmployeeDao;
               return _employeeDao;
           }
       }

        #region IEmployeeAppService Members

       /// <summary>
       /// 
       /// </summary>
       /// <param name="employeeSearchRequest"></param>
       /// <returns></returns>
        public EmployeeSearchResponse SearchEmployee(Chinook.Facade.Messaging.EmployeeSearchRequest employeeSearchRequest)
        {
            EmployeeSearchResponse result = new EmployeeSearchResponse();
            try
            {
                var employees = EmployeeDao.GetEmployeeByName(employeeSearchRequest.Name, employeeSearchRequest.PageIndex,employeeSearchRequest.PageSize);
                //var employees = EmployeeDao.GetPaged(employeeSearchRequest.PageIndex, employeeSearchRequest.PageSize);
              
                List<EmployeeViewModel> employeeViewModels = new List<EmployeeViewModel>();
                foreach (Employee e in employees)
                {
                    EmployeeViewModel employeeViewModel = new EmployeeViewModel();
                    employeeViewModel.Id = e.Id;
                    employeeViewModel.ManagerFullName = (e.ReportsTo !=null) ? e.ReportsTo.Name : string.Empty;
                    employeeViewModel.ManagerId = (e.ReportsTo != null) ? e.ReportsTo.Id : 0;
                    employeeViewModel.Address = e.Address;
                    employeeViewModel.Fullname = e.Name;
                    employeeViewModel.BirthDate = e.BirthDate;
                    employeeViewModel.Email = e.Email;
                    employeeViewModels.Add(employeeViewModel);
                }             
                result.Success = true;
                result.PageSize = employees.PageSize;
                result.PageIndex = employees.PageIndex;
                result.TotalCount = employees.TotalCount;
                result.EmployeeViewModel = employeeViewModels;

            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.Message;
            }
            return result;           
        }

       /// <summary>
       /// 
       /// </summary>
       /// <param name="lookUpEmployeeRequest"></param>
       /// <returns></returns>
        public LookUpEmployeeResponse LookupEmployee(Chinook.Facade.Messaging.LookUpEmployeeRequest lookUpEmployeeRequest)
        {
            LookUpEmployeeResponse result = new LookUpEmployeeResponse();
            try
            {
                List<Employee> employees = EmployeeDao.GetAll();
                List<EmployeeViewModel> employeeViewModels = new List<EmployeeViewModel>();
                int i = 0;
                for (i = 0; i < employees.Count; i++)
                {
                    EmployeeViewModel employeeViewModel = new EmployeeViewModel();
                    employeeViewModel.Address = employees[i].Address;
                    employeeViewModel.Fullname = employees[i].Name;
                    employeeViewModel.BirthDate = employees[i].BirthDate;
                    employeeViewModel.Email = employees[i].Email;
                    employeeViewModels.Add(employeeViewModel);
                    //UtilsFactory.Logger.Log(employees[i].ToString(), LogType.Info);                    
                }
                result.EmployeeViewModel = employeeViewModels;
                result.Success = true;

            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.Message;
            }
            return result;           
        }
        public EmployeeSaveResponse SaveEmployee(EmployeeSaveRequest employeeSaveRequest)
        {
            EmployeeSaveResponse result = new EmployeeSaveResponse();
            try
            {
                var input = employeeSaveRequest.employeeViewModel;
                Employee e = new Employee()
                {
                    Title = input.Title,
                    FirstName = input.FirstName,
                    LastName = input.LastName,
                    BirthDate = input.BirthDate,
                    StreetAddressLine1 = input.StreetAddressLine1,
                    StreetAddressLine2 = input.StreetAddressLine2,
                    City = input.City,
                    State = input.State,
                    Country = input.Country,
                    PostalCode = input.PostalCode,
                    Email = input.Email,
                    Phone = input.Phone,
                    Fax = input.Fax,
                    //ReportsTo=input.Title,
                    HireDate = input.HireDate
                };

            EmployeeDao.Save(e);
            result.Success=true;
            employeeSaveRequest.employeeViewModel.Id = e.Id;//Set the autoincrement number
            result.employeeViewModel = employeeSaveRequest.employeeViewModel;
            }
            
            catch(Exception ex)
            {
                result.Success=false;
                result.Message=ex.Message;
            }
            return result;

        }

       public EmployeePhoneGapResponse EmployeeList(EmployeePhoneGapRequest employeePhoneGapRequest)
       {
           throw new NotImplementedException();
       }

       public EmployeeDetailsPhoneGapResponse EmployeeDetails(EmployeeDetailsPhoneGapRequest employeeDetailsPhoneGapRequest)
       {
           throw new NotImplementedException();
       }

       #endregion
    }
}
